package lock.cyclicBarrier;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {

    public static void main(String[] args) {
        // 创建栅栏锁，3个任务，执行3次await()
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Runnable() {
            @Override
            public void run() {
                // 所有线程同时到达栅栏后执行任务
                System.out.println("选手各就各位。。。");
                try {
                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        });
        System.out.println("主线程开始。。。");
        // 3个任务并发执行，而不是串行
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        PlayerOneTask oneTask = new PlayerOneTask(cyclicBarrier);
        PlayerTwoTask twoTask = new PlayerTwoTask(cyclicBarrier);
        PlayerThreeTask threeTask = new PlayerThreeTask(cyclicBarrier);
        threadPool.execute(oneTask);
        threadPool.execute(twoTask);
        threadPool.execute(threeTask);

//        System.out.println("主线程操作耗时：" + (System.currentTimeMillis() - beginTime));
//        System.exit(0);
    }
}
